{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  \n",
    "import pandas as pd  \n",
    "import os  \n",
    "  \n",
    "def get_distance(file, inX):  \n",
    "#    inX = 0  \n",
    "    dataSet = pd.read_csv(file, encoding=\"utf_8_sig\",engine='python',keep_default_na = False)  \n",
    "    tree_points = dataSet[['x', 'y']]#获取样地所有点坐标的列表  \n",
    "    tree_points = tree_points.values#取坐标列表中的值  \n",
    "  \n",
    "    dataSetSize = dataSet.shape[0]#获取数据行数  \n",
    "    tree = tree_points[inX]#随机当前树  \n",
    "    diffMat = np.tile(tree, (dataSetSize, 1)) - tree_points#使当前树与其他树做减法  \n",
    "    sqDiffMat = diffMat ** 2  \n",
    "    sqDistances = sqDiffMat.sum(axis=1)  \n",
    "    distances = sqDistances ** 0.5  \n",
    "    sortedDistIndicies = distances.argsort()  \n",
    "    return [sortedDistIndicies[0:5], distances[sortedDistIndicies[0:5]]]  \n",
    "  \n",
    "def get_vector_included_angle2(tree_points,ids):  \n",
    "    central_tree_id = ids[0][0]  \n",
    "    last_four_tree_ids = ids[0][1:]  \n",
    "  \n",
    "    degrees = []  \n",
    "      \n",
    "    for id in last_four_tree_ids[0:]:  \n",
    "        first_vec = tree_points[id] - tree_points[central_tree_id]  \n",
    "        x = first_vec[0]  \n",
    "        y = first_vec[1]  \n",
    "          \n",
    "        theta = np.math.atan2(y,x)*180.0/np.pi  \n",
    "        if theta <0:  \n",
    "            theta = 360+theta  \n",
    "        degrees.append(theta)  \n",
    "    degrees.sort()  \n",
    "    angles = [degrees[1]- degrees[0],degrees[2]-degrees[1],degrees[3]-degrees[2],degrees[3]-degrees[0]]  \n",
    "    for i,_ in enumerate(angles):  \n",
    "        if angles[i]>180:  \n",
    "            angles[i] = 360-angles[i]  \n",
    "      \n",
    "    return angles  \n",
    "          \n",
    "def comparsion(degree_list):#角尺度  \n",
    "  \n",
    "    a = []  \n",
    "    for i in degree_list:  \n",
    "        if i < 72:  \n",
    "            Z = 1  \n",
    "        else:  \n",
    "            Z = 0  \n",
    "        a.append(Z)  \n",
    "    W = np.sum(a) / 4  \n",
    "    return W  \n",
    "  \n",
    "def comparison_DBH(dbhs):#大小比  \n",
    "    b = []  \n",
    "    dia_list = list(dbhs)  \n",
    "    refrence_dia = dia_list[0]  \n",
    "    for d in dia_list[1:]:  \n",
    "        if d < refrence_dia:  \n",
    "            k = 0  \n",
    "        else:  \n",
    "            k = 1  \n",
    "        b.append(k)  \n",
    "  \n",
    "    M = np.sum(b) / 4  \n",
    "    return M  \n",
    "  \n",
    "def get_average_diameter(diameter):  \n",
    "    D_list = []  \n",
    "    for i in diameter:  \n",
    "        d = i**2  \n",
    "        D_list.append(d)  \n",
    "    D = (np.sum(D_list) / len(diameter))**0.5  \n",
    "    return D  \n",
    "  \n",
    "def get_each_columns_average(dataSet):  \n",
    "    dataSet = pd.DataFrame(dataSet)  \n",
    "    list_average = []  \n",
    "    for col in dataSet.columns[5:]:  \n",
    "        average = dataSet['%s'%col].mean()  \n",
    "        list_average.append(average)  \n",
    "    dic = dict(zip(dataSet.columns[5:],list_average))  \n",
    "  \n",
    "    return dic  \n",
    "  \n",
    "def mingling(class_list):#混交度  \n",
    "    lst = []  \n",
    "    first = list(class_list)[0]  \n",
    "    for i in list(class_list)[1:]:  \n",
    "        if i == first:  \n",
    "            k = 0  \n",
    "        else:  \n",
    "            k = 1  \n",
    "        lst.append(k)  \n",
    "    h = np.sum(lst)/4  \n",
    "    return h  \n",
    "      \n",
    "      \n",
    "if __name__ == '__main__':  \n",
    "    file_path = r'./使用数据'         \n",
    "    out_path  = r'./输出数据'  \n",
    "      \n",
    "    name_list = os.listdir(file_path)      \n",
    "    #name_list.sort(key = lambda i:int(re.search(r'(\\d+)',i).group()))    \n",
    "    for j in name_list:  \n",
    "        file = os.path.join(file_path,j)  \n",
    "        dataSet = pd.read_csv(file, encoding=\"utf_8_sig\",engine='python')  \n",
    "        tree_points = dataSet[['x', 'y']]  \n",
    "        tree_points = tree_points.values  \n",
    "        dataSetSize = dataSet.shape[0]  \n",
    "        sps = dataSet[u'树种']  \n",
    "        diameter = dataSet[u'胸径']  \n",
    "        D_average = get_average_diameter(diameter)  \n",
    "          \n",
    "        idx = []  \n",
    "        tree_min_ids = []  \n",
    "        W_list = []  \n",
    "        M_list = []  \n",
    "        H_list = []  \n",
    "        for i in range(len(tree_points)):  \n",
    "    #        if (i == 6):  \n",
    "    #            print('test')  \n",
    "            ids = get_distance(file,i)  \n",
    "            idx.append(ids)  \n",
    "              \n",
    "            angles = get_vector_included_angle2(tree_points,idx[i])  \n",
    "            H = mingling(sps[ids[0]])  \n",
    "            H_list.append(H)  \n",
    "            W = comparsion(angles)  \n",
    "            W_list.append(W)  \n",
    "            U = comparison_DBH(diameter[ids[0]])  \n",
    "            M_list.append(U)  \n",
    "        mean = get_each_columns_average(dataSet)  \n",
    "      \n",
    "        out_df = pd.DataFrame()  \n",
    "        out_df['树种'] = sps  \n",
    "        out_df['角尺度'] = W_list  \n",
    "        out_df['大小比'] = M_list  \n",
    "        out_df['混交度'] = H_list  \n",
    "        out_df.to_csv(os.path.join(out_path,'{}空间结构.csv'.format(j[:-4])),encoding = 'utf_8_sig',index = False)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>树种</th>\n",
       "      <th>角尺度</th>\n",
       "      <th>大小比</th>\n",
       "      <th>混交度</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>麻栎</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>杉木</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>马尾松</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>麻栎</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.25</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>枫香</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.25</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>麻栎</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>麻栎</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.50</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>杉木</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>杉木</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>马尾松</td>\n",
       "      <td>0.50</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>枫香</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>麻栎</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>马尾松</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>麻栎</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>麻栎</td>\n",
       "      <td>0.25</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>杉木</td>\n",
       "      <td>0.50</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>麻栎</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>枫香</td>\n",
       "      <td>0.25</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>马尾松</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.25</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>枫香</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     树种   角尺度   大小比   混交度\n",
       "0    麻栎  0.50  0.50  0.50\n",
       "1    杉木  0.50  0.00  0.75\n",
       "2   马尾松  0.50  0.75  1.00\n",
       "3    麻栎  0.75  0.25  1.00\n",
       "4    枫香  0.25  0.25  1.00\n",
       "5    麻栎  0.75  0.75  0.50\n",
       "6    麻栎  0.50  0.50  1.00\n",
       "7    杉木  0.50  0.00  1.00\n",
       "8    杉木  0.50  0.75  1.00\n",
       "9   马尾松  0.50  1.00  0.75\n",
       "10   枫香  0.50  0.00  0.50\n",
       "11   麻栎  0.50  0.25  0.50\n",
       "12  马尾松  1.00  0.75  1.00\n",
       "13   麻栎  0.50  0.25  0.00\n",
       "14   麻栎  0.25  1.00  0.00\n",
       "15   杉木  0.50  1.00  0.75\n",
       "16   麻栎  0.75  0.00  0.00\n",
       "17   枫香  0.25  1.00  1.00\n",
       "18  马尾松  0.50  0.25  1.00\n",
       "19   枫香  0.75  0.25  0.75"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "out_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
